package order

import (
	"context"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mxshop-api/order-web/api"
	"mxshop-api/order-web/forms/request"
	"mxshop-api/order-web/forms/response"
	"mxshop-api/order-web/global"
	"mxshop-api/order-web/proto"
	"net/http"
	"strconv"
)

/*
*
创建订单
*/
func New(ctx *gin.Context) {

	//1 判断参数
	createOrderForm := request.CreateOrderForm{}
	err := ctx.ShouldBindJSON(&createOrderForm)
	if err != nil {
		zap.S().Errorf("执行下单,参数异常:%v", err)
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	//2 下单获取订单号和订单id
	userId, _ := ctx.Get("userId")
	order, err := global.OrderSrvClient.CreateOrder(context.WithValue(context.Background(), "ginContext", ctx), &proto.OrderRequest{
		UserId:  int32(userId.(uint)),
		Address: createOrderForm.Address,
		Name:    createOrderForm.Name,
		Mobile:  createOrderForm.Mobile,
		Post:    createOrderForm.Post,
	})
	if err != nil {
		zap.S().Errorf("调用下单服务返回异常:%v", err)
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	//3 生成支付宝支付链接地址
	var alipayUrl string = ""

	//4 返回结果
	ctx.JSON(http.StatusOK, gin.H{
		"alipay_url": alipayUrl,
		"id":         order.Id,
		"orderSn":    order.OrderSn,
	})
}

/*
*
订单详情
*/
func Detail(ctx *gin.Context) {
	idStr := ctx.Param("id") //这个id就是goodsId
	id, err := strconv.Atoi(idStr)
	if err != nil {
		zap.S().Errorf("请求url格式不正确")
		ctx.JSON(http.StatusOK, gin.H{
			"msg": "请求url格式不正确",
		})
		return
	}
	orderDetail, err := global.OrderSrvClient.OrderDetail(context.WithValue(context.Background(), "ginContext", ctx), &proto.OrderRequest{Id: int32(id)})
	if err != nil {
		zap.S().Errorf("调用下单服务获取订单详情,返回异常:%v", err)
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	orderDetailRes := response.OrderDetailRes{}
	var orderGoodsVos []response.OrderGoodVo
	orderDetailVo := response.OrderDetailVo{
		Id: 0,
	}
	orderDetailRes.OrderDetailVo = orderDetailVo
	for _, good := range orderDetail.Goods {
		orderGoodsVos = append(orderGoodsVos, response.OrderGoodVo{
			Id:    good.GoodsId,
			Name:  good.GoodsName,
			Image: good.GoodsImage,
			Price: good.GoodsPrice,
			Nums:  good.Nums,
		})
	}
	orderDetailRes.OrderGoodVos = orderGoodsVos
	ctx.JSON(http.StatusOK, orderDetailRes)
}

/*
*
获取订单列表
*/
func List(ctx *gin.Context) {
	userId, _ := ctx.Get("userId")
	pagesStr := ctx.Param("pages")
	pagePerNumsStr := ctx.Param("pagePerNums")
	var pages int
	var err error
	if pagesStr == "" {
		pages = 1
	} else {
		pages, err = strconv.Atoi(pagesStr)
		if err != nil {
			ctx.JSON(http.StatusNotFound, gin.H{
				"msg": "参数错误:pages",
			})
			zap.S().Error("参数错误:pages")
			return
		}
	}
	var pagePerNums int
	if pagePerNumsStr == "" {
		pagePerNums = 1
	} else {
		pagePerNums, err = strconv.Atoi(pagePerNumsStr)
		if err != nil {
			ctx.JSON(http.StatusNotFound, gin.H{
				"msg": "参数错误:pagePerNums",
			})
			zap.S().Error("参数错误:pagePerNums")
			return
		}
	}
	list, err := global.OrderSrvClient.OrderList(context.WithValue(context.Background(), "ginContext", ctx), &proto.OrderFilterRequest{
		UserId:      int32(userId.(uint)),
		Pages:       int32(pages),
		PagePerNums: int32(pagePerNums),
	})
	if err != nil {
		zap.S().Errorf("调用下单服务返回异常:%v", err)
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	var orderListVoRes = response.OrderListVoRes{}
	var orderListVos = make([]response.OrderListVo, 0)
	for _, OrderInfoResponse := range list.Data {
		orderVo := response.OrderListVo{
			Id:      OrderInfoResponse.Id,
			UserId:  OrderInfoResponse.UserId,
			OrderSn: OrderInfoResponse.OrderSn,
			PayType: OrderInfoResponse.PayType,
			Status:  OrderInfoResponse.Status,
			Post:    OrderInfoResponse.Post,
			Total:   OrderInfoResponse.Total,
			Address: OrderInfoResponse.Address,
			Name:    OrderInfoResponse.Name,
			Mobile:  OrderInfoResponse.Mobile,
			AddTime: OrderInfoResponse.AddTime,
		}
		orderListVos = append(orderListVos, orderVo)
	}
	orderListVoRes.Total = list.Total
	orderListVoRes.OrderListVos = orderListVos
	ctx.JSON(http.StatusOK, orderListVoRes)
}
